package cn.huanba.controller;

import cn.huanba.dto.Result;
import cn.huanba.utils.FileUtils;
import cn.huanba.utils.OssUtils;
import cn.huanba.utils.ResultUtils;
import cn.huanba.utils.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.UUID;

/**
 * 阿里oss
 */
@RestController
@RequestMapping("/admin/file")
@Api(value = "图片上传控制类", tags = "图片上传", description = "图片上传模块控制类描述")
public class FileController {
    @Autowired
    private OssUtils ossUtils;

    @RequestMapping("/upload")
    @ApiOperation(value = "图片上传", notes = "图片上传至阿里oss")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "图片文件", paramType = "upload", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "filePath", value = "oss路径", paramType = "String", required = true, dataType = "Integer")
    })
    public Result upload(@RequestParam("file") MultipartFile file, String filePath) {
        File upFile = FileUtils.MultipartFileToFile(file);
        String newFilename = UUID.randomUUID().toString().replace("-", "");
        ossUtils.fileUpload(filePath+"/", newFilename, upFile);
        upFile.delete();
        String tempUrl = ossUtils.createFileName("type/" + newFilename);
        return ResultUtils.returnDataSuccess(StringUtil.createSimpleMap("url", tempUrl));
    }
}
